﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace PCLawPrototype
{
    public partial class ManageMatter : ManageRibbonForm
    {
        int iSelectedIndex = -1;
        List<Matter> _matters = new List<Matter>();

        public ManageMatter()
        {
            InitializeComponent();
            this.WindowState = FormWindowState.Maximized;

            FillToGrid();
        }

        private void FillToGrid()
        {
            _matters = Common.Entity.Matters.ToList();
            lstClient.Items.Clear();

            foreach (Matter matter in _matters)
            {
                matter.ClientReference.Load();

                ListViewItem listViewItem1 = new ListViewItem(new string[] {
                    (_matters.IndexOf(matter) + 1).ToString(),
                    matter.MatterDescription,
                    matter.Client.ClientName,
                    matter.CreatedDate.ToShortDateString(),
                    matter.MatterStatus == 1 ? "Open" : "Close" }, -1);

                this.lstClient.Items.Add(listViewItem1);
            }
        }

        private void lstClient_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (lstClient.SelectedIndices.Count > 0)
            {
                iSelectedIndex = lstClient.SelectedIndices[0];

                if (lstClient.Items[lstClient.SelectedIndices[0]].SubItems[4].Text == "Open")
                {
                    ((DStarter)this.MdiParent).ChangeActiveText("Deac&tive");
                }
                else
                {
                    ((DStarter)this.MdiParent).ChangeActiveText("Ac&tive");

                }
            }
            else
                iSelectedIndex = -1;
        }

        private void ManageMatter_FormClosing(object sender, FormClosingEventArgs e)
        {
            this.Hide();
            e.Cancel = true;
            ((DStarter)this.MdiParent).SetRPEventVisible(false);
        }

        #region Overrides of ManageRibbonForm

        public override void Add()
        {
            NewMatterFrm matterForm = new NewMatterFrm();
            matterForm.ShowDialog();

            Reload();
        }

        public override void Edit()
        {
            NewMatterFrm frmMatterClient;
            if (lstClient.SelectedItems.Count > 0)
            {
                if (lstClient.SelectedIndices.Count > 0)
                {
                    frmMatterClient = new NewMatterFrm(_matters[lstClient.SelectedIndices[0]]);
                    frmMatterClient.ShowDialog();
                    Reload();
                }
            }
        }

        public override void Delete()
        {
            if (lstClient.SelectedIndices.Count > 0)
            {
                Common.Entity.DeleteObject(_matters[lstClient.SelectedIndices[0]]);
                Common.Entity.SaveChanges();
                lstClient.Items.RemoveAt(lstClient.SelectedIndices[0]);
            }
        }

        public override void Reload()
        {
            //MessageBox.Show("Reload data from database and fill data to list!", "Description for function", MessageBoxButtons.OK, MessageBoxIcon.Information);
            Common.Entity.Refresh(System.Data.Objects.RefreshMode.ClientWins, Common.Entity.Matters);
            FillToGrid();
        }

        public override void Active()
        {
            if (((DStarter)this.MdiParent).GetActiveText() == "Ac&tive")
            {
                lstClient.Items[iSelectedIndex].SubItems[4].Text = "Open";
                lstClient.Items[iSelectedIndex].BackColor = Color.White;
                _matters[iSelectedIndex].MatterStatus = 1;
            }
            else
            {
                lstClient.Items[iSelectedIndex].SubItems[4].Text = "Close";
                lstClient.Items[iSelectedIndex].BackColor = Color.FromArgb(80, 150, 150, 150);
                _matters[iSelectedIndex].MatterStatus = 0;
            }
            Common.Entity.SaveChanges();
        }

        public override void ViewTask()
        {
            ManageTask manageTask = new ManageTask();
            manageTask.ShowDialog();
        }
        #endregion

        private void ManageMatter_Activated(object sender, EventArgs e)
        {
            ((DStarter)this.MdiParent).SetRPGMatterVisible(true);
            ((DStarter)this.MdiParent).SetEventFocus();
        }

        private void ManageMatter_Deactivate(object sender, EventArgs e)
        {
            ((DStarter)this.MdiParent).SetRPGMatterVisible(false);
        }

        private void lstClient_DoubleClick(object sender, EventArgs e)
        {
            NewMatterFrm frmMatterClient;
            if (lstClient.SelectedItems.Count > 0)
            {
                if (lstClient.SelectedIndices.Count > 0)
                {
                    frmMatterClient = new NewMatterFrm(_matters[lstClient.SelectedIndices[0]]);
                    frmMatterClient.ShowDialog();
                    Reload();
                }
            }
        }

        private void lstClient_Click(object sender, EventArgs e)
        {
            ((DStarter)this.MdiParent).SetEventFocus();
        }

    }
}
